#include "esp_task_manager.h"

#include <esp_log.h>

#define TAG "TaskManager"

// TaskManager 实现
TaskManager& TaskManager::GetInstance() {
  static TaskManager instance;
  return instance;
}

std::shared_ptr<TaskExecutor> TaskManager::GetExecutor(const std::string& name) {
    std::lock_guard<std::mutex> lock(executors_mutex_);
    auto it = executors_.find(name);
    
    if (it != executors_.end()) {
        return it->second;
    } else {
        return nullptr;
    }
}

std::shared_ptr<TaskExecutor> TaskManager::CreateExecutor(const std::string& name, uint32_t stack_size, UBaseType_t priority, BaseType_t core_id) {
    std::lock_guard<std::mutex> lock(executors_mutex_);
    // 检查是否已存在
    auto it = executors_.find(name);
    if (it != executors_.end()) {
        ESP_LOGW(TAG, "执行器已存在: %s", name.c_str());
        return it->second;
    }
    
    // 创建新执行器
    auto executor = std::make_shared<TaskExecutor>(name, stack_size, priority, core_id);
    executors_[name] = executor;
    
    ESP_LOGI(TAG, "执行器创建完成: %s", name.c_str());
    return executor;
}
